/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view.commons;

import org.apache.commons.validator.GenericValidator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
import org.mags.remas.control.RelationshipEditorInput;
import org.mags.remas.exception.SingletonExceptionHandler;
import org.mags.remas.model.entity.EntityRelationship;

/**
 * 
 * @author Mario Gaitan
 *
 */
public abstract class RelationshipEditorBase extends EditorPart {
	protected EntityRelationship entityRelationship;
	@Override
	public Image getDefaultImage() {
		Image image = null;
		if(entityRelationship!=null) 
			image = ImageProvider.getEntityImage(entityRelationship.getEntity().getEntityType().getCode().toLowerCase()+".png");
		if(image==null)
			image = ImageProvider.getCommonImage("new.png");			
		return image;
	}	
	@Override
	public void doSave(IProgressMonitor monitor) {		
	}
	@Override
	public void doSaveAs() {
	}
	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		super.setSite(site);
		super.setInput(input);
		entityRelationship = ((RelationshipEditorInput)input).getEntityRelationship();
		setNormalPartName();
		init(input);
	}
	protected void setNormalPartName() {
		if(entityRelationship!=null) {
			if(GenericValidator.isBlankOrNull(entityRelationship.getEntity().getName()))
				super.setPartName("*"+entityRelationship.getEntity().getEntityType().getName());
			else
				super.setPartName(entityRelationship.getEntity().getName()+" ["+entityRelationship.getEntity().getCode()+"]");
		}		
	}
	public abstract void init(IEditorInput input);
	@Override
	public boolean isDirty() {
		return false;
	}
	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}
	@Override
	public abstract void createPartControl(Composite parent);
	public abstract boolean ignoreException(Exception e);
	@Override
	public abstract void setFocus();
	public void exceptionCatched(Exception e) {
		if(!ignoreException(e))
			SingletonExceptionHandler.getInstance().showException(e);
	}	
	public void closeMe() {
		getSite().getPage().closeEditor(this, true);
	}	
}
